﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.ResumeService
{
    public static class LanguageAbilityManager
    {
        public static void SaveLanguageAbility(Guid _id,
            int _ability,
            int _language,
            int _read,
            int _write,
            string _user)
        {
            try
            {
                #region 入口检测
                if (string.IsNullOrEmpty(_user)) throw new Exception("用户名不能为空");
                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        var _l = _resume.LanguageAbility.Where(l => l.ID == _id).FirstOrDefault();
                        if (_l == null)
                        {
                            if (_resume.LanguageAbility.Count() > 10) throw new Exception("语言能力记录每人最多可发10条");
                            _l = new LanguageAbility();
                            _l.ID = Guid.NewGuid();
                            _l.AbilityID = _ability;
                            _l.LanguageID = _language;
                            _l.Read = _read;
                            _l.Write = _write;
                            _l.User = _user;

                            _adc.LanguageAbility.InsertOnSubmit(_l);
                        }
                        else
                        {
                            _l.AbilityID = _ability;
                            _l.Read = _read;
                            _l.Write = _write;
                        }
                        _resume.UpdateDate = DateTime.Now;
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存语言能力信息出错：" + _e.Message);
            }
        }
        /// <summary>
        /// 获取语言能力
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static LanguageAbility GetLanguageAbility(Guid _id)
        {
            return DBHelper.Get<LanguageAbility>(l => l.ID == _id, "获取语言能力对象出错：");
        }
        /// <summary>
        /// 删除语言能力
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static void Delete(Guid _id)
        {
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _l = _adc.LanguageAbility.Where(l => l.ID == _id).FirstOrDefault();
                    if (_l == null) throw new Exception("记录不存在，请刷新后重试。");
                    _adc.LanguageAbility.DeleteOnSubmit(_l);
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("删除语言能力记录出错：" + _e.Message);
            }
        }
        /// <summary>
        /// 获取语言能力列表
        /// </summary>
        /// <param name="_user"></param>
        /// <returns></returns>
        public static List<LanguageAbility> GetLanguageAbility(string _user)
        {
            if (string.IsNullOrEmpty(_user)) throw new Exception("用户名不能为空");
            return DBHelper.GetAll<LanguageAbility>(la => la.User == _user, "获取语言能力列表出错:");
        }
    }
}
